package algorithm;

public class leetcode_top100_04 {


    public static void main(String[] args) {

        int[] a={1,3};
        int[] b={2};
        System.out.println(findMedianSortedArrays(a,b));
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {

        double result=0;
        int[] temp=new int[nums1.length+nums2.length];
        int p1=0;
        int p2=0;
        int p=0;
        while(p1<nums1.length && p2<nums2.length){
            if (nums1[p1]<nums2[p2]){
                temp[p++]=nums1[p1++];
            }else{
                temp[p++]=nums2[p2++];
            }
        }

        while(p1<nums1.length){
            temp[p++]=nums1[p1++];
        }
        while(p2<nums2.length){
            temp[p++]=nums2[p2++];
        }
        int len=temp.length;
        if(len%2==0){ //偶数
            int a=len/2;
            int b=a-1;
            result=((double) temp[a]+(double) temp[b])/2;
        }else{
            result=temp[len/2];
        }

        return result;
    }
}
